package com.example.nonggo.model;

import android.util.Log;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.example.nonggo.Constants;
import com.example.nonggo.api.BaseApi;
import com.example.nonggo.api.MDataObserver;
import com.example.nonggo.bean.BdsoundDTO;
import com.example.nonggo.bean.BdsoundVO;
import com.example.nonggo.contract.QuestionContract;
import com.example.nonggo.utils.FileUtils;
import com.tencent.mmkv.MMKV;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import okhttp3.RequestBody;

public class QuestionModel extends BaseModel<QuestionContract.Presenter>{
    private final String aiURL= "https://zhiwen.mancloud.cn/api/app-api/chat/sysv1/wear-chat-messages";

    public void getWearChatMessages(String query, String area, String user/*, QuestionContract.Presenter callback*/) {
        RxHttpUtils.
                createApi(BaseApi.class).getWearChatMessages(aiURL, "Bearer " + token, getWearChatMessagesBD(query, area, user))
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<String>() {

                    @Override
                    protected void onSuccess(String data) {
                        callback.wearChatMessages(data);
                    }
                });

    }
    private RequestBody getWearChatMessagesBD(String query, String area, String user) {
        JSONObject jb = new JSONObject();
        try {
            JSONObject inputs = new JSONObject();
            inputs.put("zhiwen_user_mobile", user);
            inputs.put("area", area);
            inputs.put("tag", "wearos");
            jb.put("inputs", inputs);
            jb.put("query", query);
            jb.put("response_mode", "blocking");
            jb.put("conversation_id", "");
            jb.put("user", user);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRequestBody(jb);
    }

    public void fileTranslateText(String absolutePath) {
        Log.e("QuestionModel", "transcribeAudio: " + absolutePath);
        String s = FileUtils.fileToBase64(absolutePath);
        File convertedFile = new File(absolutePath);
        String token = MMKV.defaultMMKV().getString(Constants.ACCESS_TOKEN, "0");
        RxHttpUtils.createApi(BaseApi.class)
                .bdsound(token, new BdsoundDTO(s, convertedFile.length()).toRequestBody())
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<>() {
                    @Override
                    protected void onSuccess(BdsoundVO data) {
                        if (data.getResult() == null) {
                            return;
                        }
                        callback.translateText(data.getResult()[0]);
                    }

                    @Override
                    public void doOnCompleted() {
                        super.doOnCompleted();
                        try {
                            // 根据路径删除文件
                            if (convertedFile.exists()) {
                                convertedFile.delete();
                            }
                        } catch (Exception ignore) {

                        }
                    }

                    @Override
                    public void doOnError(String errorMsg) {
                        callback.translateText("");
                    }
                });
    }
}
